<project basedir="." default="war_honban">
	<property file="build.properties" />

	<macrodef name="init">
		<sequential>
			<property name="work.dir" location="work/" />
			<property name="build.dir" location="${work.dir}/build/" />
			<property name="build.bin.dir" location="${build.dir}/bin/" />
			<property name="build.lib.dir" location="${build.dir}/" />
			<property name="build.src.dir" location="${build.dir}/bin/" />
			<property name="build.web.dir" location="${build.dir}/web/" />

			<property name="target.dir" location="target/" />
			<property name="target.src.dir" location="${target.dir}/java/" />
			<property name="target.web.dir" location="${target.dir}/web/" />
			<property name="target.webinf.dir" location="${target.web.dir}/WEB-INF/" />
			<property name="target.lib.dir" location="${target.webinf.dir}/lib/" />
			<available property="target.lib.exists" file="${target.lib.dir}" />
			<available property="target.src.exists" file="${target.src.dir}" />

			<property name="deprecated.dir" value="deprecated" />
			<property name="deprecated.lib.dir" value="${deprecated.dir}/lib/" />
			<property name="deprecated.src.dir" value="${deprecated.dir}/java/" />
			<available property="deprecated.lib.exists" file="${deprecated.lib.dir}" />
			<available property="deprecated.src.exists" file="${deprecated.src.dir}" />

			<property name="java.dir" location="C:/Program Files/Java/jdk1.8.0_25/" />
			<property name="compile.exe" location="${java.dir}/bin/javac.exe" />
			<property name="compile.encode" value="UTF8" />
			<property name="compile.version" value="1.8" />
			<property name="jar.exe" location="${java.dir}/bin/jar.exe" />
			<property name="war.exe" value="${jar.exe}" />

			<property name="target.jars" value="" />
			<property name="deprecated.jars" value="" />



			<property name="targetos" value="windows" />
			<path id="project.name.tmp1">
				<dirset dir="." excludes="**\\*" />
			</path>
			<property name="project.name.tmp2" location=".." />
			<pathconvert targetos="${targetos}" property="project.name" refid="project.name.tmp1">
				<map from="${project.name.tmp2}/" to="" />
				<map from="${project.name.tmp2}" to="" />
			</pathconvert>
			<property name="project.subname" value="" />

			<property name="jar.main.class" value="" />
			<property name="jar.manifest.name" value="jar.mani.txt" />
			<property name="jar.manifest.path" location="${build.dir}/${jar.manifest.name}" />
			<property name="jar.name" value="${project.name}${project.subname}.zip" />
			<property name="jar.path" location="${target.dir}/${jar.name}" />
			<property name="build.user.name" value="${user.name}" />
			<property name="war.name" value="${project.name}${project.subname}.war" />
			<property name="war.path" value="${target.dir}/${war.name}" />
			<property name="webxml.path" location="${build.dir}/web.xml" />

			<property name="initialized" value="true" />
		</sequential>
	</macrodef>
	<macrodef name="clean">
		<sequential>
			<delete dir="${build.dir}" />
		</sequential>
	</macrodef>

	<macrodef name="copysrc.target2build">
		<sequential>
			<echo>copy src. [target -&gt; build]</echo>
			<copy todir="${build.src.dir}">
				<fileset dir="${target.src.dir}" />
			</copy>
		</sequential>
	</macrodef>
	<macrodef name="copylib.target2build">
		<sequential>
			<echo>copy lib. [target -&gt; build]</echo>
			<antcall target="_copylib.target2build.if" />
			<antcall target="_copylib.target2build.unless" />
		</sequential>
	</macrodef>
	<target name="_copylib.target2build.if" if="${target.lib.exists}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<copy todir="${build.lib.dir}">
			<fileset dir="${target.lib.dir}" />
		</copy>
	</target>
	<target name="_copylib.target2build.unless" unless="${target.lib.exists}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>non copy [${target.lib.dir} is not exists.]</echo>
	</target>

	<macrodef name="copylib.deprecated2build">
		<sequential>
			<echo>copy lib. [deprecated -&gt; build]</echo>
			<antcall target="_copylib.deprecated2build.if" />
			<antcall target="_copylib.deprecated2build.unless" />
		</sequential>
	</macrodef>
	<target name="_copylib.deprecated2build.if" if="${deprecated.lib.exists}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>copy ${deprecated.lib.dir} -&gt; ${build.lib.dir}</echo>
		<copy todir="${build.lib.dir}">
			<fileset dir="${deprecated.lib.dir}" />
		</copy>
	</target>
	<target name="_copylib.deprecated2build.unless" unless="${deprecated.lib.exists}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>non copy [${deprecated.lib.dir} is not exists.]</echo>
	</target>

	<macrodef name="copysrc.deprecated2build">
		<sequential>
			<echo>copy src. [deprecated -&gt; build]</echo>
			<antcall target="_copysrc.deprecated2build.if" />
			<antcall target="_copysrc.deprecated2build.unless" />
		</sequential>
	</macrodef>
	<target name="_copysrc.deprecated2build.if" if="${deprecated.src.exists}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>copy ${deprecated.src.dir} -&gt; ${build.src.dir}</echo>
		<copy todir="${build.src.dir}">
			<fileset dir="${deprecated.src.dir}" />
		</copy>
	</target>
	<target name="_copysrc.deprecated2build.unless" unless="${deprecated.src.exists}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>non copy [${deprecated.src.dir} is not exists.]</echo>
	</target>

	<macrodef name="copyjar.extra2build">
		<sequential>
			<path id="extra.target.jars">
				<filelist files="${target.jars}" />
				<filelist files="${deprecated.jars}" />
			</path>
			<copy todir="${build.lib.dir}" flatten="true">
				<path refid="extra.target.jars" />
			</copy>
		</sequential>
	</macrodef>

	<target name="compile">
		<init />
		<clean />
		<mkdir dir="${build.bin.dir}" />
		<mkdir dir="${build.src.dir}" />
		<mkdir dir="${build.lib.dir}" />

		<copylib.deprecated2build />
		<copysrc.deprecated2build />
		<copylib.target2build />
		<copysrc.target2build />
		<copyjar.extra2build />

		<path id="target.classpath">
			<fileset dir="${build.lib.dir}" includes="**/*.jar,**/*.zip" />
		</path>
		<pathconvert targetos="${targetos}" property="target.classpath.string" refid="target.classpath" pathsep=";">
			<map from="${build.lib.dir}/" to="" />
			<map from="${build.lib.dir}" to="" />
		</pathconvert>
		<echo>javac version="${compile.version}" encode="${compile.encode}" classpath="${target.classpath.string}"</echo>
		<javac srcdir="${build.src.dir}" destdir="${build.bin.dir}" includeantruntime="false" debug="true" fork="yes" encoding="${compile.encode}" executable="${compile.exe}" source="${compile.version}" target="${compile.version}">
			<classpath refid="target.classpath" />
			<include name="**/*.java" />
		</javac>
	</target>

	<target name="jartask" depends="compile">
		<delete file="${jar.path}" />
		<jar jarfile="${jar.path}" compress="true" index="false" basedir="${build.bin.dir}">
			<include name="**/*.class" />
			<include name="**/*.java" />
			<manifest>
				<attribute name="Built-By" value="${build.user.name}" />
				<attribute name="Class-Path" value="${target.classpath.string}" />
				<attribute name="Created-By" value="${compile.version}" />
				<attribute name="Main-Class" value="${jar.main.class}" />
			</manifest>
		</jar>
		<clean />
	</target>

	<target name="jar" depends="compile">
		<delete file="${jar.path}" />

		<path id="jar.classpath">
			<fileset dir="${build.lib.dir}" includes="**/*.jar,**/*.zip" />
		</path>
		<pathconvert targetos="${targetos}" property="jar.classpath.string" refid="jar.classpath" pathsep=" ">
			<map from="${build.lib.dir}/" to="" />
			<map from="${build.lib.dir}" to="" />
		</pathconvert>
		<concat append="false" destfile="${jar.manifest.path}">Built-By: ${build.user.name}
Main-Class: ${jar.main.class}
Class-Path: ${jar.classpath.string}
</concat>

		<property name="tmpjarcmd">"${jar.exe}" cfm ${jar.path} ${jar.manifest.path} .</property>
		<echo>create jar</echo>
		<echo>  ${tmpjarcmd}</echo>
		<exec executable="${tmpjarcmd}" dir="${build.bin.dir}" />
		<clean />
	</target>

	<target name="war" depends="jar">
		<create_war prefix="" />
	</target>
	<target name="war_honban" depends="jar">
		<create_war prefix="honban." />
	</target>
	<target name="war_staging" depends="jar">
		<create_war prefix="staging." />
	</target>
	<target name="war_development" depends="jar">
		<create_war prefix="staging." />
	</target>
	<macrodef name="create_war">
		<attribute name="prefix" />
		<sequential>
			<delete file="${war.path}" />

			<copy todir="${build.web.dir}">
				<fileset dir="${target.web.dir}" />
			</copy>
			<copy todir="${build.web.dir}/WEB-INF/lib" file="${jar.path}" />



			<available property="exists.replace.properties" file="@{prefix}replace.properties" />
			<available property="exists.replace.config.properties" file="@{prefix}replace.config.properties" />
			<available property="exists.replace.html.properties" file="@{prefix}replace.html.properties" />
			<available property="exists.replace.jsp.properties" file="@{prefix}replace.jsp.properties" />
			<available property="exists.replace.properties.properties" file="@{prefix}replace.properties.properties" />
			<available property="exists.replace.xml.properties" file="@{prefix}replace.xml.properties" />
			<echo>file contents replace.</echo>
			<echo>  replace rule</echo>
			<echo>    \u000a &lt;- line feed</echo>
			<echo>    \u000d &lt;- carriage return</echo>
			<echo>    \u0020 &lt;- space</echo>
			<echo>    \u002e &lt;- period[.]</echo>
			<echo>    \u003a &lt;- colon[:]</echo>
			<echo>    \u003d &lt;- equal[=]</echo>
			<echo>    \u003f &lt;- question[?]</echo>
			<antcall target="_replace_all">
				<param name="prefix">@{prefix}</param>
			</antcall>
			<antcall target="_replace_config">
				<param name="prefix">@{prefix}</param>
			</antcall>
			<antcall target="_replace_html">
				<param name="prefix">@{prefix}</param>
			</antcall>
			<antcall target="_replace_jsp">
				<param name="prefix">@{prefix}</param>
			</antcall>
			<antcall target="_replace_properties">
				<param name="prefix">@{prefix}</param>
			</antcall>
			<antcall target="_replace_xml">
				<param name="prefix">@{prefix}</param>
			</antcall>



			<property name="tmpwarcmd">"${war.exe}" cfM ${war.path} .</property>
			<echo>create war</echo>
			<echo>  ${tmpwarcmd}</echo>
			<exec executable="${tmpwarcmd}" dir="${build.web.dir}" />

			<clean />
		</sequential>
	</macrodef>
	<target name="_replace_all" if="exists.replace.properties">
		<fail unless="initialized" />
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace.properties" summary="on">
			<include name="**/*.config" />
			<include name="**/*.html" />
			<include name="**/*.jsp" />
			<include name="**/*.properties" />
			<include name="**/*.xml" />
		</replace>
	</target>
	<target name="_replace_config" if="exists.replace.config.properties">
		<fail unless="initialized" />
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace.config.properties" summary="on">
			<include name="**/*.config" />
		</replace>
	</target>
	<target name="_replace_html" if="exists.replace.html.properties">
		<fail unless="initialized" />
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace.html.properties" summary="on">
			<include name="**/*.html" />
		</replace>
	</target>
	<target name="_replace_jsp" if="exists.replace.jsp.properties">
		<fail unless="initialized" />
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace.jsp.properties" summary="on">
			<include name="**/*.jsp" />
		</replace>
	</target>
	<target name="_replace_properties" if="exists.replace.properties.properties">
		<fail unless="initialized" />
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace.properties.properties" summary="on">
			<include name="**/*.properties" />
		</replace>
	</target>
	<target name="_replace_xml" if="exists.replace.xml.properties">
		<fail unless="initialized" />
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace.xml.properties" summary="on">
			<include name="**/*.xml" />
		</replace>
	</target>
</project>